<!-- 巡检打卡 -->
<template>
	<view>
		<view class="no-warpper" v-if="isLong">
			<view v-if="hasInfo">
				<view class="scroll-box">
					<view class="nl-shadow-box card-box" v-for="(item,index) in patrolList" :key="index">
						<view class="nl-flex-list title">
							<view>{{item.taskId}}</view>
							<text :style="item.state == '20200405'? 'color:#AFAFAF' : 'color:#1585FC'">{{item.stateName}}</text>
						</view>
						
						<view class="nl-flex-list">
							<view class="content">
								<view class="item">
									<view>巡检计划</view>
									<text>：</text>
									<view>{{item.inspectionPlanName}}</view>
								</view>
								<view class="item">
									<view>计划编码</view>
									<text>：</text>
									<view>{{item.inspectionPlanId}}</view>
								</view>
								<view class="item">
									<view>巡检人</view>
									<text>：</text>
									<view>{{item.departmentName}}</view>
								</view>
								<view class="item">
									<view>巡检时间</view>
									<text>：</text>
									<view>{{item.planInsTime}}</view>
								</view>
								<view class="item">
									<view>巡检方式</view>
									<text>：</text>
									<view>{{item.signTypeName}}</view>
								</view>
							</view>
						</view>

						<!-- 按钮 -->
						<view class="botton-flex">
							<view class="button-mini main m8" @click="Patrol(item.taskId)">
								<button>{{item.state == '20200405' ? '开始巡检':'继续巡检'}}</button>
							</view>
						</view>

					</view>
					<u-loadmore :status="status" />
				</view>
			</view>
			<!-- 无数据 渲染缺省图 -->
			<view v-if="hasInfo == false">
				<noData :noDataText="noDataText"></noData>
			</view>
		</view>

		<view v-else>
			<netError v-if="!hasNet" @refresh="refresh()"></netError>
		</view>
	</view>
</template>

<script>
	import {
		getListInspectionTasks
	} from 'api/patrol';
	import {
		mapState
	} from 'vuex';

	export default {
		data() {
			return {
				listBox: [], // 避免上拉刷新时，页面出现空白
				patrolList: [],

				page: 1,
				records: 0,
				status: 'loadmore',

				isLong: false, // 是否加载完成
				hasNet: true, // 是否有网络
				hasInfo: '', // 是否有数据
				noDataText: '暂无数据~',
			}
		},
		computed: {
			...mapState('user', ['userInfo', 'userCommunity'])
		},
		onLoad() {
			this.page = 1
			this.records = 0
			this.listBox = []
			this.getListInspectionTasks(1)
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.records = 0
			this.listBox = []
			this.getListInspectionTasks(1)
		},
		// 上拉加载
		onReachBottom(){
			console.log('页面触底了')
			if (this.status == 'loading') { // 防止多次触发
			
			} else {
				this.status = 'loading'
				if (this.page <= this.records) {
					this.getListInspectionTasks(this.page)
				} else {
					this.status = 'nomore'
				}
			}
		},
		methods: {
			/**
			 * 获取巡检列表
			 */
			getListInspectionTasks(page) {
				let that = this
				let data = {
					communityId: this.userCommunity.communityId,
					planUserId: this.userInfo.userId,
					moreState: '20200405,20200406',
					page: page,
					row: 10
				}
				getListInspectionTasks(data).then((res) => {
					console.log('获取巡查列表', res)

					if (res.records == 0) { //暂无数据
						that.hasInfo = false
						that.isLong = true
						uni.hideLoading()
						uni.stopPullDownRefresh()
						return
					}

					that.records = res.records
					if (that.records == that.page) {
						that.status = 'nomore'
					} else {
						that.status = 'loadmore'
					}
					that.page++
					uni.stopPullDownRefresh()
					for (let i of res.inspectionTasks) {
						that.listBox.push(i)
					}
					that.patrolList = that.listBox
					
					that.isLong = true
					that.hasInfo = true

				}).catch(function(reason) {
					uni.stopPullDownRefresh()
					// 处理网络错误
					that.hasNet = false
					that.isLong = false
				})
			},
			/**
			 * 巡检按钮
			 */
			Patrol(taskId) {
				uni.navigateTo({
					url:`../patrolProcess/patrolProcess?taskId=${taskId}`
				})
			},
			/**
			 * 刷新
			 */
			refresh() {
				this.page = 1
				this.records = 0
				this.listBox = []
				this.getListInspectionTasks(1)
			}
		}
	}
</script>

<style lang="scss">
	.repair-content {
		width: 100%;
		background-color: #F3F9FF;
		padding: 24rpx;
		border-radius: 8rpx;

		.repair-item {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			font-size: $uni-font-size-base;
			margin-bottom: 12rpx;

			view:nth-child(1) {
				width: 140rpx;
				text-align-last: justify;
				color: $nl-text-color-grey;
			}

			text {
				color: $nl-text-color-grey;
			}

			view:nth-of-type(2) {
				flex: 1;
			}
		}

		.repair-item:last-child {
			align-items: flex-start;
			margin-bottom: none
		}
	}

	.scroll-box {
		width: 100%;
		padding-bottom: 32rpx;
	}
</style>
